﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml.Linq;
using tiDE.API;
using System.Runtime.Serialization.Formatters.Binary;
using tiDE.Runtime;

namespace tiDE
{
    public partial class CreateFileDialog : Form
    {
        public CreateFileDialog(string SelectedDirectory, string Filter)
        {
            InitializeComponent();
            ProjectDirectory = SelectedDirectory;
            this.Filter = Filter;
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        string ProjectDirectory, Filter;
        bool hasNameChanged = false, isChangeHandled = false;

        private void CreateProjectDialog_Load(object sender, EventArgs e)
        {
            // Load all the templates
            string[] templateDefs = Directory.GetFiles(Directory.GetCurrentDirectory() + "\\Templates\\FileTemplates", "*.tideft");
            List<string> defs = new List<string>(templateDefs);
            defs.AddRange(Directory.GetFiles(RuntimeData.tiDEUserDirectory + "\\Templates\\FileTemplates", "*.tideft"));
            templateDefs = defs.ToArray();
            StreamReader reader;
            foreach (string template in templateDefs)
            {
                reader = new StreamReader(template);
                XDocument templateXml = XDocument.Parse(reader.ReadToEnd());
                XElement templateElement = templateXml.Element("FileTemplate");
                string templateName = "", templateCat = "Default Category";
                if (templateElement.Attribute("Name") != null)
                    templateName = templateElement.Attribute("Name").Value;
                else
                    continue;
                if (templateElement.Attribute("Category") != null)
                    templateCat = templateElement.Attribute("Category").Value;
                if (templateElement.Attribute("Language") != null)
                {
                    if (templateElement.Attribute("Language").Value == Filter)
                    {
                        imageList1.Images.Add(Image.FromFile("Templates\\default.ico"));
                        ListViewItem item = new ListViewItem(templateName, imageList1.Images.Count - 1);
                        item.Tag = templateElement;
                        bool groupFound = false;
                        foreach (ListViewGroup group in listView1.Groups)
                        {
                            if (group.Header == templateCat)
                            {
                                item.Group = group;
                                groupFound = true;
                                break;
                            }
                        }
                        if (!groupFound)
                        {
                            ListViewGroup group = new ListViewGroup(templateCat);
                            item.Group = group;
                            listView1.Groups.Add(group);
                        }
                        listView1.Items.Add(item);
                    }
                }
            }
            isChangeHandled = true;
            listView1.Items[0].Selected = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                // If no type of project is selected
                button1.Enabled = false;
                return;
            }
            else
                // If any kind of project is selected
                button1.Enabled = true;

            XElement element = (XElement)listView1.SelectedItems[0].Tag;
            if (element.Attribute("Description") != null)
                label4.Text = element.Attribute("Description").Value;
            else
                label4.Text = "";

            string extension = "";
            if (element.Attribute("Extension") != null)
                extension = element.Attribute("Extension").Value;

            if (!hasNameChanged)
            {
                isChangeHandled = true;
                string projectName = listView1.SelectedItems[0].Text.Replace(" ", "");
                int projectNameOffset = 1;
                while (File.Exists(projectName + projectNameOffset.ToString()))
                {
                    projectNameOffset++;
                }
                textBox1.Text = projectName + projectNameOffset.ToString() + extension;
            }
        }

        public Solution Solution;
        public string FileName;

        private void button1_Click(object sender, EventArgs e)
        {
            XElement templateElement = listView1.SelectedItems[0].Tag as XElement;

            FileName = textBox1.Text;

            if (File.Exists(ProjectDirectory + textBox1.Text))
            {
                MessageBox.Show("File already exists.\nPlease select another location.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            StreamWriter writer = new StreamWriter(ProjectDirectory + "\\" + FileName);
            writer.Write(templateElement.Value);
            writer.Close();
            FileName = ProjectDirectory + "\\" + FileName;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        public string SolutionFile;
    }
}
